// pages/clock/pages/jobText/index.js
const {
	http
} = require("../../../../utils/apiutils");
const util = require("../../../../utils/util")
const app = getApp()

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		jobData: {},
		id: 0,
		index: 0,
		videoList: [],
		showbord: false,
		percentage:0,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			id:options.id
		})
		this.getJobData(options.id)
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		this.videoContext = wx.createVideoContext('myVideo')
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},
	getJobData: function (id) {
		var that = this;
		wx.showLoading({
			title: '加载中...',
		})
		http.getJobData(id,
			function (res) {
				wx.hideLoading()
				if (res.code == 0) {
					let videoList = [];
					// if (res.data.videoList.length > 0) {
					// 	res.data.videoList.map((item,index) => { 
					// 		// if (index < 3) { 
					// 			videoList.push({id:index+1,url:item.src})
					// 		// }
					// 	})
					// }
					let percentage = parseInt(res.data.registrationNumber/res.data.number*100)
					
					that.setData({
						jobData: res.data,
						videoList,
						percentage:percentage<2?2:percentage
					})
					wx.nextTick(_ => { 
						// that.setVideoData()
					})
				} else {
					wx.showToast({
						title: res.message,
						icon: "none",
					})
				}
			},
			function (err) {
				wx.hideLoading()
			})
	},
	setVideoData() { 
		this.videoContext.play()

	},
	videochange(e) { 
		this.setData({
			index:e.detail.activeId
		})
	},
	swiperChange(e) { 
		this.setData({
			index: e.detail.current
		});
		this.clickfunc();
		this.getJobData(this.data.id)
	},
	funcClick(e) {
		let key = e.currentTarget.dataset.key * 1;
		let _this = this;
		switch (key) {
			case 1:
				// _this.setData({
				// 	showbord: true
				// })
				// setTimeout(_ => {
				// 	_this.setData({
				// 		showbord: false
				// 	})
				// }, 2000)
				break;
			case 2:
				wx.navigateBack({
				  delta: 1,
				})
				break;
			case 3:
				wx.redirectTo({
					url:'/pages/clock/pages/jobvideo/index?id='+this.data.jobData.id
				})
				break;
			case 4:
				wx.redirectTo({
					url:'/pages/clock/pages/jobimg/index?id='+this.data.jobData.id
				})
				break;
		}
	},
	clickfunc(e) {
		let params = {
			mediaId:this.data.jobData.videoList[this.data.index].id,
			type: e?e.currentTarget.dataset.type:0
		}
		http.savemediaId(params, (res) => {
			if (res.status == 200) { 
				// if (this.data.jobData.videoList[this.data.index].isLike) { 
				if(!params.type) return
				let videoList = [...this.data.jobData.videoList];
				videoList[this.data.index].isLike = !videoList[this.data.index].isLike
				if (videoList[this.data.index].isLike) {
					videoList[this.data.index].likeNum = videoList[this.data.index].likeNum + 1;
				} else { 
					videoList[this.data.index].likeNum = videoList[this.data.index].likeNum - 1;
				}
					this.setData({
						['jobData.videoList']:[...videoList]
					})
				// }
			}
		 }, err => { 
			
		})
	},
	onShareAppMessage: function () {
		var that = this;
		// 设置菜单中的转发按钮触发转发事件时的转发内容
		var shareObj = {
			title:this.data.jobData.name, // 默认是小程序的名称(可以写slogan等)
			path: '/pages/index/index?type=jobvideo&id=' + this.data.jobData.id , // 默认是当前页面，必须是以‘/'开头的完整路径
			imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
		}
		this.data.shareTime = new Date().getTime();
		return shareObj;

	},
	videoerror(e) {
		console.log(e);
	},
	
})